import Vue from 'vue'
import VueRouter from 'vue-router'
/* 先导入需要路由的位置 */
import home from '../views/home/index.vue'
import login from '../views/login/index.vue'
import jiegou from '../views/jichusjgl/jiegou.vue'
import shouye from '../views/home/index2.vue'
import store from '@/store'
import silingsi from '@/views/silingsi/silingsi.vue'

Vue.use(VueRouter)
const whitePage = ['/silingsi', '/login']
const routes = [
  {
    path: '/',
    redirect: '/login' // 重定向到 /home 路由
  },
  {
    path: '/home',
    name: 'home',
    component: home,
    children: [
      {
        path: '/shouye',
        name: 'shouye',
        component: shouye
      },
      {
        path: '/jiegou',
        name: 'jiegou',
        component: jiegou
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/silingsi',
    name: 'silingsi',
    component: silingsi
  }

]

const router = new VueRouter({
  routes
})
/* 路由前置 */
router.beforeEach((to, from, next) => {
  /* 做个判断如果在有token的情况下去的是登录页的话就打回到首页 */
  /* 如果有没有token就打回登录页面 */
  if (store.state.user.token) {
    if (to.path.toLowerCase() === '/login') {
      /* 打回首页 */
      next('/shouye')
    } else {
      next()
    }
  } else {
    if (whitePage.includes(to.path.toLowerCase())) {
      next()
    } else {
      next('/login')
    }
  }
})

/* 路由后置守卫 */
router.afterEach((to, from) => {
})

export default router
